{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "AttributeError",
     "evalue": "'TextRank' object has no attribute 'get_nodes'",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-14-4c9287b60e61>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mnumpy\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mnp\u001b[0m   \u001b[1;31m#导入所需要的库与模块\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      2\u001b[0m \u001b[1;32mimport\u001b[0m \u001b[0mjieba\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mposseg\u001b[0m \u001b[1;32mas\u001b[0m \u001b[0mpseg\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 3\u001b[1;33m \u001b[1;32mclass\u001b[0m \u001b[0mTextRank\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mobject\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m     \u001b[1;31m#定义TextRank类\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      4\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m__init__\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mwindow\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0md\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0miters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      5\u001b[0m         \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtext\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m<ipython-input-14-4c9287b60e61>\u001b[0m in \u001b[0;36mTextRank\u001b[1;34m()\u001b[0m\n\u001b[0;32m     81\u001b[0m         \u001b[0mtext\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'冬奥会会徽以汉字“冬”为灵感来源，运用中国书法的艺术形态， 将厚重的东方文化底蕴与国际化的现代风格融为一体，呈现出新时代的中国新形象、新梦想，传递出新时代中国为办好北京冬奥会，圆冬奥之梦，实现“三亿人参与冰雪运动”目标，圆体育强国之梦，推动世界冰雪运动发展，为国际奥林匹克运动做出新贡献的不懈努力和美好追求。会徽图形上半部分展现滑冰运动员的造型，下半部分表现滑雪运动员的英姿。中间舞动的线条流畅且充满韵律，代表举办地起伏的山峦、赛场、冰雪滑道和节日飘舞的丝带，为会徽增添了节日喜庆的视觉感受，也象征着北京冬奥会将在中国春节期间举行。会徽以蓝色为主色调，寓意梦想与未来，以及冰雪的明亮纯洁。红黄两色源自中国国旗，代表运动的激情、青春与活力。在“BEIJING 2022”字体的形态上汲取了中国书法与剪纸的特点，增强了字体的文化内涵和表现力，也体现了与会徽图形的整体感和统一性。'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     82\u001b[0m         \u001b[0mTextRank\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTextRank\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtext\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0.85\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m100\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 83\u001b[1;33m         \u001b[0mTextRank\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_nodes\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m     \u001b[1;31m#创建节点\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     84\u001b[0m         \u001b[0mTextRank\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_matrix\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m    \u001b[1;31m#创建邻接矩阵\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     85\u001b[0m         \u001b[0mTextRank\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mcount_weight\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m  \u001b[1;31m#计算节点权重\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mAttributeError\u001b[0m: 'TextRank' object has no attribute 'get_nodes'"
     ]
    }
   ],
   "source": [
    "import numpy as np   #导入所需要的库与模块\n",
    "import jieba.posseg as pseg\n",
    "class TextRank(object):     #定义TextRank类\n",
    "    def __init__(self, text, window, d, iters):\n",
    "        self.text = text\n",
    "        self.window = window\n",
    "        self.d = d\n",
    "        self.side_dict = {}\n",
    "        self.iters = iters\n",
    "    #构建节点集合\n",
    "    def get_nodes(self):\n",
    "        tag_filter = ['a', 'd', 'n', 'v']\n",
    "        cut_word = pseg.cut(self.text)   #对文本进行分词\n",
    "        filter_list = [s.word for s in cut_word if s.flag in tag_filter]  #过滤词性\n",
    "        stop_word_path = 'data/stops_list.txt'   #加载停用词表\n",
    "        stopword_list = [sw.replace('\\n', '') for sw in \n",
    "                         open(stop_word_path, 'r', encoding = 'utf-8').readlines()]\n",
    "        self.word_list = []\n",
    "        for word in filter_list:\n",
    "            if not word in stopword_list and len(word) > 1:\n",
    "                self.word_list.append(word)\n",
    "                win_list = []\n",
    "        wlist_len = len(self.word_list)\n",
    "        #遍历每个词，获取其索引和值\n",
    "        for index, word in enumerate(self.word_list):\n",
    "            #判断词是否已经处理\n",
    "            if word not in self.side_dict.keys():\n",
    "                win_list.append(word)\n",
    "                win_set = set()\n",
    "                #计算窗口的左边界和右边界\n",
    "                left = index - self.window + 1\n",
    "                right = index + self.window\n",
    "                #规范窗口边界，确保边界不会超出词表的范围\n",
    "                if left < 0:\n",
    "                    left = 0\n",
    "                if right >= wlist_len:\n",
    "                    right = wlist_len\n",
    "                #遍历窗口范围内的词\n",
    "                for i in range(left, right):\n",
    "                    #判断当前遍历的索引是否为当前词的索引\n",
    "                    if i == index:\n",
    "                        continue\n",
    "                    else:         #将该词作为当前词的一个邻居\n",
    "                        win_set.add(self.word_list[i])\n",
    "                    #将当前词及其邻居存储在字典中\n",
    "                    self.side_dict[word] = win_set\n",
    "    #构建一个表示无向图的邻接矩阵\n",
    "    def get_matrix(self):\n",
    "        #初始化一个矩阵\n",
    "        self.matrix = np.zeros([len(set(self.word_list)),len(set(self.word_list))])\n",
    "        self.word_index = {}\n",
    "        self.word_dict = {}\n",
    "        #建立从词到索引的映射和从索引到词的映射\n",
    "        for i,v in enumerate(set(self.word_list)):\n",
    "            self.word_index[v] = i\n",
    "            self.word_index[i] = v\n",
    "        for key in self.side_dict.keys():    #构建无向图\n",
    "            for w in self.side_dict[key]:\n",
    "                #建立无向边，有边为1，无边为0\n",
    "                self.matrix[self.word_index[key]][self.word_index[w]] = 1\n",
    "                self.matrix[self.word_index[w]][self.word_index[key]] = 1\n",
    "    #计算节点权重\n",
    "    def count_weight(self):\n",
    "        sum = np.sun(self.matrix, axis=0)   #按列求和\n",
    "        #遍历矩阵的每一个元素，将其除以相应列的和，实现每列的归一化\n",
    "        for j in range(self.matrix.shape[1]):\n",
    "            for i in range(self.matrix.shape[0]):\n",
    "                self.matrix[i][j] /= sum[j]\n",
    "        #初始化一个全为1的向量\n",
    "        self.node_vec = np.ones([len(set(self.word_list))])\n",
    "        for i in range(self.iters):     #迭代更新\n",
    "            self.node_vec = (1 - self.d) + self.d * np.dot(self.matrix, self.node_vec)\n",
    "            textR_dict = {}\n",
    "        for i in range(len(self.node_vec)):\n",
    "            textR_dict[self.index_dict[i]] = self.node_vec[i]\n",
    "            #根据字典中的值进行降序排列\n",
    "            res = sorted(textR_dict.items(), key = lambda x: x[1], reverse = True)\n",
    "            print('TextRank算法关键词提取的结果：')\n",
    "            print(res[:10])\n",
    "    if __name__  == '__main__':\n",
    "        text = '冬奥会会徽以汉字“冬”为灵感来源，运用中国书法的艺术形态， 将厚重的东方文化底蕴与国际化的现代风格融为一体，呈现出新时代的中国新形象、新梦想，传递出新时代中国为办好北京冬奥会，圆冬奥之梦，实现“三亿人参与冰雪运动”目标，圆体育强国之梦，推动世界冰雪运动发展，为国际奥林匹克运动做出新贡献的不懈努力和美好追求。会徽图形上半部分展现滑冰运动员的造型，下半部分表现滑雪运动员的英姿。中间舞动的线条流畅且充满韵律，代表举办地起伏的山峦、赛场、冰雪滑道和节日飘舞的丝带，为会徽增添了节日喜庆的视觉感受，也象征着北京冬奥会将在中国春节期间举行。会徽以蓝色为主色调，寓意梦想与未来，以及冰雪的明亮纯洁。红黄两色源自中国国旗，代表运动的激情、青春与活力。在“BEIJING 2022”字体的形态上汲取了中国书法与剪纸的特点，增强了字体的文化内涵和表现力，也体现了与会徽图形的整体感和统一性。'\n",
    "        TextRank = TextRank(text, 3, 0.85, 100)\n",
    "        TextRank.get_nodes()     #创建节点\n",
    "        TextRank.get_matrix()    #创建邻接矩阵\n",
    "        TextRank.count_weight()  #计算节点权重"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
